import { request, config } from 'ice'
import { gzip } from 'pako'
async function getData({ ugmId }) {
  return request.post(`${config.nodeURL}/get_data`, { ugmId })
}

async function getDataFromAil({ url, toBase64 }) {
  return request.post(`${config.nodeURL}/get_data_from_ail`, { url, toBase64 })
}

function zip(data) {
  return gzip(JSON.stringify(data), { to: 'string' })
}

async function saveData({ ugmId, data, type }) {
  return request.post(`${config.nodeURL}/save_data`, zip({ ugmId, data, type }), {
    headers: {
      'Content-Type': 'application/json',
      'Content-Encoding': 'gzip'
    }
  })
}

async function divideRoom({ ugmId, stage, mergeList, type }) {
  return request.post(`${config.nodeURL}/node_divideRoom`, {
    taskId: ugmId,
    stage,
    mergeList,
    url: config.baseURL,
    type,
    isFromWeb: true
  }, {timeout: 30*60*1000})
}

async function planningScanstation({ points, stage, selectedIds, ugm, ugmId, type }) {
  return request.post(`${config.nodeURL}/node_planningScanstation`, {
    points,
    ugmId,
    ugm,
    stage,
    selectedIds,
    url: config.baseURL,
    type,
    isFromWeb: true
  })
}

async function queryByTaskId({ taskId }) {
  return request.post(`${config.mingDaoURL}/queryByTaskId`, {
    taskId
  })
}

async function queryByPlanningId({ planningId }) {
  return request.post(`${config.mingDaoURL}/queryByPlanningId`, {
    planningId
  })
}

async function queryByInstallmentId({ installmentId }) {
  return request.post(`${config.mingDaoURL}/queryByInstallmentId`, {
    installmentId
  })
}

async function getDrawingLayoutPoint({ storeyId }) {
  return request.post(`${config.mingDaoURL}/getDrawingLayoutPoint`, {
    storey_id: storeyId
  })
}

async function getDrawingLayoutPointFromBackup({ storeyId, drawingId }) {
  return request.post(`${config.mingDaoURL}/getDrawingLayoutPointFromBackup`, {
    storey_id: storeyId,
    drawing_id: drawingId
  })
}

async function updateRoomsNumAndLayoutData({ drawingId, stageId, data }) {
  return request.post(`${config.mingDaoURL}/updateRoomsNumAndLayoutData`, {
    drawingId,
    data,
    stage_id: stageId
  })
}

async function getIfcData({ ifcId }) {
  return request.post(`${config.nodeURL}/get_ifc_data`, { ifcId })
}

const BIMApi = {
  getData,
  getDataFromAil,
  saveData,
  divideRoom,
  planningScanstation,
  queryByTaskId,
  queryByInstallmentId,
  getDrawingLayoutPoint,
  getDrawingLayoutPointFromBackup,
  updateRoomsNumAndLayoutData,
  queryByPlanningId,
  getIfcData
}

export default BIMApi
